import 'dart:math';

import 'package:common_utils/common_utils.dart';

import '../core/logger.dart';

// 邮箱判断
bool isEmail(String input) {
  const regexEmail =
      r'^([a-z0-9A-Z]+[-|\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\.)+[a-zA-Z]{2,}$';
  if (input.isEmpty) {
    return false;
  }
  return RegExp(regexEmail).hasMatch(input);
}

bool isNotEmpty(String? text) {
  return text?.isNotEmpty ?? false;
}

bool isEmpty(String? text) {
  return text?.isEmpty ?? true;
}

String numStrFormat(String num) {
  return numFormat(stringToNum(num).toDouble());
}

String numFormat(double num) {
  const b = 100000000.0;
  const k = 1000.0;
  // const double m = 1000000.0;
  const w = 10000.0;
  if (num >= b) {
    final value = num / b;
    var result = NumUtil.getNumByValueDouble(value, 2)?.toStringAsFixed(2);
    result = result?.replaceAll('.00', '');
    return '$result亿';
  }
  /*else if (num >= m) {
    var value = num / m;
    var result = (NumUtil.getNumByValueDouble(value, 2))?.toStringAsFixed(2);
    result = result?.replaceAll(".00", "");
    return "$result百万";
  } */
  else if (num >= w) {
    final value = num / w;
    var result = NumUtil.getNumByValueDouble(value, 2)?.toStringAsFixed(2);
    result = result?.replaceAll('.00', '');
    return '$result万';
  } else if (num >= k) {
    final value = num / k;
    var result = NumUtil.getNumByValueDouble(value, 2)?.toStringAsFixed(2);
    result = result?.replaceAll('.00', '');
    return '$result千';
  }
  return num.toInt().toString();
}

int stringToNum(String? str) {
  try {
    final num = int.parse(str ?? '0');
    return num;
  } catch (e) {
    CoreKitLogger().d(e.toString());
  }
  return 0;
}

String generateRandomString(int length) {
  final random = Random();
  const availableChars =
      'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
  final randomString = List.generate(length,
      (index) => availableChars[random.nextInt(availableChars.length)]).join();

  return randomString;
}
